/*
 * CommonIndexingPolicy.cc
 *
 *  Created on: Mar 9, 2013
 *      Author: neo
 */
#include <cassert>

#include "../index/InvertedIndex.h"
#include "../report-model/AbstractBugReport.h"
#include "../util/MacroUtility.h"
#include "CommonIndexingPolicy.h"

CommonIndexingPolicy::CommonIndexingPolicy(int max_term_id,
    IndexingType::EnumIndexingType type) :
    AbstractIndexingPolicy(new InvertedIndex(max_term_id)), m_type(type) {
  assert(this->m_type != IndexingType::NO_INDEXING);
}

CommonIndexingPolicy::~CommonIndexingPolicy() {
}

IndexingType::EnumIndexingType CommonIndexingPolicy::get_report_candidates(
    const AbstractBugReport& query_report,
    vector<AbstractBugReport*>& candidate_collector) const {
  assert(candidate_collector.empty());
  const vector<Term>& terms = query_report.get_all_unigrams().get_terms();
  this->get_index()->get_reports(terms, candidate_collector);
  assert(false);
  return this->m_type;
}

void CommonIndexingPolicy::update_index(AbstractBugReport& bug_report) const {
  const vector<Term>* terms;
  switch (this->m_type) {
  case IndexingType::SUMMARY_INDEXING:
    terms = &(bug_report.get_summary_unigrams().get_terms());
    break;
  case IndexingType::DESCRIPTION_INDEXING:
    terms = &(bug_report.get_description_unigrams().get_terms());
    break;
  case IndexingType::FULL_INDEXING:
    terms = &(bug_report.get_all_unigrams().get_terms());
    break;
  default: {
    UNREACHABLE(IndexingType::get_indexing_type_string(this->m_type).c_str());
    return;
  }
  }
  this->add_report(*terms, bug_report);
}
